<?php
require_once('entities/UserGroup.php');

class GroupModel
{
	public $group;
	public function __construct()
	{
		$this->group = new UserGroup();
	}
	public function create($data)
	{
		global $entityManager;
		$this->group->setTitle($data['title']);
		$this->group->setParentId($data['parent_id']);
		$this->group->setStatus($data['status']);
		$this->group->setParameters($data['parameters']);
		$this->group->setNotes($data['notes']);

		$entityManager->persist($this->group);
		$entityManager->flush();
		header('Location: index.php?com=com_user&view=groups');
		exit();
	}
	public function update($data)
	{
		global $entityManager;
		if(empty($data['id']))
		{
			header('Location: index.php?com=com_user&view=groups');
			exit();
		}
		$this->group->setId($data['id']);
		
		$this->group->setTitle($data['title']);
		$this->group->setParentId($data['parent_id']);
		$this->group->setStatus($data['status']);
		$this->group->setParameters($data['parameters']);
		$this->group->setNotes($data['notes']);

		$entityManager->merge($this->group);
		$entityManager->flush();
		header('Location: index.php?com=com_user&view=groups');
		exit();
	}
	
	public function getGroupById($id){
		global $entityManager;
		return $entityManager->getRepository('UserGroup')->findBy( array('id' => $id ));
		
	}
	public function optionParent($root, $string = '', &$result) {
		global $entityManager;
		$groupsList = $entityManager->getRepository('UserGroup')->findBy(array('parent_id' => $root));
		foreach ($groupsList as $groups) {
			if ($groups->getId() == 1) {
				$result .= '<option value="'. $groups->getId() .'">'.$groups->getTitle().'</option>';
			} else {
				$result .= '<option value="'. $groups->getId() .'">' . $string. $groups->getTitle() . '</option>';
			}
			$this->optionParent($groups->getId(),$string . '&nbsp;&nbsp;', $result);
		}
	}
	// public function check_exists_user($username){
	// 	global $entityManager;
	// 	$results = $entityManager->getRepository('User')->findBy( array('username' => $username ));
	// 	if ($results) {
	// 		return true;
	// 	}
	// 	else return false;
	// }
	// public function login($username,$password)
	// {
	// 	global $entityManager;
	// 	$results = $entityManager->getRepository('User')->findBy( array('username' => $username , 'password' => $password ));
	// 	 if($results) 
	// 	{
	// 		foreach ($results as $result) {
	// 			$_SESSION['username'] = $result->getUserName();
	// 			header('Location: index.php?com=com_user&view=info');
	// 		}
			
	// 	}
	// }
}